package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("travelCommend")
public class TravelCommendController {

    @Autowired
    private ITravelCommendService commendService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") TravelCommendQuery qo, Model model){
        model.addAttribute("result",commendService.selectForList(qo));
        return "travelCommend/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(TravelCommend travelCommend, MultipartFile file){
         JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
               // String imgUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PAGE + "upload");
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                travelCommend.setCoverUrl(imgUrl);
            }
            commendService.saveOrUpdate(travelCommend);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long commendId){
        JSONResult result = new JSONResult();
        try {
            commendService.delete(commendId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
